package cn.oms.manage.service.impl;

import cn.oms.commons.ex.ServiceException;
import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.web.ServiceCode;
import cn.oms.manage.dao.persist.repository.ICustomerRepository;
import cn.oms.manage.dao.persist.repository.IDepartmentRepository;
import cn.oms.manage.dao.persist.repository.IDoctorRepository;
import cn.oms.manage.pojo.entity.Department;
import cn.oms.manage.pojo.param.DepartmentAddNewParam;
import cn.oms.manage.pojo.param.DepartmentUpdateParam;
import cn.oms.manage.pojo.vo.DepartmentListVO;
import cn.oms.manage.pojo.vo.DepartmentStandardVO;
import cn.oms.manage.service.IDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 说明：
 *
 * @author: Fcx
 * @date: 2023/6/19
 **/
@Service
@Slf4j
public class DepartmentServiceImpl implements IDepartmentService {
    @Value("${oms.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    IDepartmentRepository departmentRepository;

    @Autowired
    ICustomerRepository customerRepository;

    @Autowired
    IDoctorRepository doctorRepository;

    @Override
    public void addNew(DepartmentAddNewParam departmentAddNewParam) {
        log.debug("开始处理【添加科室】的业务，参数：{}", departmentAddNewParam);
        // 调用参数对象的getName()得到尝试添加的科室的名称
        String name = departmentAddNewParam.getName();
        // 调用Mapper对象的countByName()执行统计查询
        int count = departmentRepository.countByName(name);
        log.debug("根据名称【{}】统计数量，结果：{}", name, count);
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "添加科室失败，科室名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 创建Department对象
        Department department = new Department();
        // 调用BeanUtils.copyProperties(源,目标)将参数对象中的属性复制到Department对象中
        BeanUtils.copyProperties(departmentAddNewParam, department);
        // 调用Mapper对象的insert()执行插入科室数据
        log.debug("即将执行插入数据，参数：{}", department);
        departmentRepository.insert(department);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除科室】的业务，参数：{}", id);
        // 调用Mapper对象的getStandardById()执行查询
        DepartmentStandardVO department = departmentRepository.getStandardById(id);
        log.debug("根据ID={}检查科室数据是否存在，查询结果：{}", id, department);
        // 判断查询结果是否为null
        if (department == null) {
            String message = "删除科室失败，尝试删除的科室数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        //TODO 检查是否与客户关联
        int count = customerRepository.countByDepartmentId(id);
        if (count > 0) {
            String message = "删除科室失败，仍有客户与该科室关联！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        //TODO 检查是否与医生关联
        count = doctorRepository.countByDepartmentId(id);
        if (count > 0) {
            String message = "删除科室失败，仍有医生与该科室关联！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        // 调用Mapper对象的deleteById()方法执行删除
        log.debug("即将执行删除，参数：{}", id);
        departmentRepository.deleteById(id);
    }

    @Override
    public void updateInfoById(Long id, DepartmentUpdateParam departmentUpdateInfoParam) {
        log.debug("开始处理【修改科室详情】的业务，ID：{}，新数据：{}", id, departmentUpdateInfoParam);
        // 调用Mapper对象的getStandardById()执行查询
        DepartmentStandardVO queryResult = departmentRepository.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：抛出异常
            String message = "修改科室详情失败，尝试修改的科室数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        // 调用Mapper对象的countByNameAndNotId()执行统计
        int count = departmentRepository.countByNameAndNotId(id, departmentUpdateInfoParam.getName());
        // 判断统计结果是否大于0
        if (count > 0) {
            // 是：名称被占用，抛出异常
            String message = "修改科室详情失败，科室名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        customerRepository.updateDepartmentNameByDepartmentId(id,departmentUpdateInfoParam.getName());

        // 创建Department对象
        Department department = new Department();
        // 复制属性，设置ID
        BeanUtils.copyProperties(departmentUpdateInfoParam, department);
        department.setId(id);
        // 调用Mapper对象的update()方法执行修改
        departmentRepository.update(department);

    }

    @Override
    public DepartmentStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询科室详情】的业务，参数：{}", id);
        DepartmentStandardVO queryResult = departmentRepository.getStandardById(id);
        // DepartmentStandardVO queryResult = departmentRedisRepository.get(id);
        if (queryResult == null) {
            // 是：抛出异常
            String message = "查询科室详情失败，科室数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<DepartmentListVO> list(Integer pageNum) {
        log.debug("开始处理【查询科室列表】的业务，页码：{}", pageNum);
        PageData<DepartmentListVO> pageData = departmentRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<DepartmentListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询科室列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<DepartmentListVO> pageData = departmentRepository.list(pageNum, pageSize);
        return pageData;
    }
}
